<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Home\Controller;
use plugins\payment\wechat\WechatSubmit;
use Home\Logic\Jssdk;
/**
 * 文档模型控制器
 * 文档模型列表和详情
 */
class ArticleController extends HomeController {

    /* 文档模型频道页 */
	public function index(){
		/* 分类信息 */
		$category = $this->category();

		//频道页只显示模板，默认不读取任何内容
		//内容可以通过模板标签自行定制

		/* 模板赋值并渲染模板 */
		$this->assign('category', $category);
		$this->display($category['template_index']);
	}

	/* 文档模型列表页 */
	public function lists($p = 1){
		/* 分类信息 */
		$category = $this->category();

		/* 获取当前分类列表 */
		$Document = D('Document');
		$list = $Document->page($p, $category['list_row'])->lists($category['id']);
		if(false === $list){
			$this->error('获取列表数据失败！');
		}

		/* 模板赋值并渲染模板 */
		$this->assign('category', $category);
		$this->assign('list', $list);
        FPC($category);
		$this->display($category['template_lists']);
	}

	/* 文档模型详情页 */
	public function detail($id = 0, $p = 1){
		/* 标识正确性检测 */
		if(!($id && is_numeric($id))){
			$this->error('文档ID错误！');
		}

		/* 页码检测 */
		$p = intval($p);
		$p = empty($p) ? 1 : $p;

		/* 获取详细信息 */
		$Document = D('Document');
		$info = $Document->detail($id);
        /*检查是否登录*/
 		if(!session("user_auth")){
 			$this->error('您尚未登录，请登陆后查看',U('User/login'));
 		}
        //获得当前栏目阅读权限
 		$Category=M('category')->where('id='.$info['category_id'])->find();

		if($Category['permissions']=='1'){
            $where['id'] = UID;
            $result = M('ucenter_member')->where($where)->find();
            if($result['level']!='1'){
                $this->error('您还没没权限阅读请升级VIP等级');
            }
		}

		//判断当前用户是否收藏
        $data=array();
		$data['resource_id']=I('id');
		$data['uid']=UID;
		$data['type']=0;
        $collection=M('member_resource')->where($data)->find();
        if($collection){
            $msg='已收藏';
        }else{
            $msg='收藏';
        }
        $this->assign('msg',$msg);

		if(!$info){
			$this->error($Document->getError());
		}

        if(UID){
            action_log('read_category', 'category', $info['category_id'], UID);
            action_log('read_article', 'article', $id, UID);
        }

		/* 分类信息 */
		$category = $this->category($info['category_id']);

		/* 获取模板 */
		if(!empty($info['template'])){//已定制模板
			$tmpl = $info['template'];
		} elseif (!empty($category['template_detail'])){ //分类已定制模板
			$tmpl = $category['template_detail'];
		} else { //使用默认模板
			$tmpl = 'Article/'. get_document_model($info['model_id'],'name') .'/detail';
		}
        # 获取相关文章
        $where = array();
        $where['category_id'] = $info['category_id'];
        $where['id'] = array('neq', $info['id']);
        $correlationList = $Document->where($where)->order('view desc, id desc')->limit(3)->select();
        foreach($correlationList as &$arr){
            $article = M('DocumentArticle')->field('bookmark')->find($arr['id']);
            $arr = array_merge($arr, $article);
        }
        $this->assign('correlationList', $correlationList);
        # END-获取相关文章
        $Picture = D('Picture');
        $pic = $Picture->where('id='.$info['cover_id'])->find();
        $this->assign('pic', $pic); //页码
		/* 更新浏览数 */
		$map = array('id' => $id);
		$Document->where($map)->setInc('view');
		/* 模板赋值并渲染模板 */
		$this->assign('cate', $category);
		$this->assign('info', $info);
		$this->assign('prev', $Document->prev($info));  // 上一篇
		$this->assign('next', $Document->next($info));  // 下一篇
		$this->assign('page', $p); //页码
		$this->display($tmpl);
	}

	/* 文档分类检测 */
	private function category($id = 0){
		/* 标识正确性检测 */
		$id = $id ? $id : I('get.category', 0);
		if(empty($id)){
			$this->error('没有指定文档分类！');
		}

		/* 获取分类信息 */
		$category = D('Category')->info($id);
		if($category && 1 == $category['status']){
            return $category;
//			switch ($category['display']) {
//				case 0:
//					$this->error('该分类禁止显示！');
//					break;
//				//TODO: 更多分类显示状态判断
//				default:
//					return $category;
//			}
		} else {
			$this->error('分类不存在或被禁用！');
		}
	}

	/**
	 * 添加收藏
	 */
	public function ajaxAddCollect(){
		$ajaxResult = array();
        $articleId = I('article_id');


        if(!UID){
            $ajaxResult['error'] = -1;
            $ajaxResult['msg'] = "您尚未登录!";
            $ajaxResult['url'] = U('User/login');
            die(json_encode($ajaxResult));
        }

        //判断vip等级
        $info= M('document')->where('id='.$articleId)->find();
        $Category=M('category')->where('id='.$info['category_id'])->find();

        if($Category['permissions']=='1'){
            $where['id'] = UID;
            $result = M('ucenter_member')->where($where)->find();
            if($result['level']!='1'){
                $ajaxResult['error'] = -5;
                $ajaxResult['msg'] = "您还没没权限阅读请升级VIP等级!";
                die(json_encode($ajaxResult));
            }
        }


		//var_dump($articleId);exit;
		$where = array();
		$where['uid'] = UID;
		$where['resource_id'] = $articleId;
		$where['type'] = 0;	// 文章类型
		$result = M('MemberResource')->where($where)->find();
		if($result){
            $remove=M('MemberResource')->where($where)->delete();
            $a = M('DocumentArticle')->where(array('id'=>$articleId))->setInc('bookmark', -1);
            if($remove){
                $ajaxResult['error'] = -2;
                $ajaxResult['msg'] = "您已取消收藏当前文章!";
                die(json_encode($ajaxResult));
            }
		}
		# 添加收藏数据

		$data = $where;
		$data['time']=time();
		$result = M('MemberResource')->add($data);
		if(empty($result)){
			$ajaxResult['error'] = -3;
			$ajaxResult['msg'] = "添加用户收藏失败!";
			die(json_encode($ajaxResult));
		}
		# END-添加收藏数据
		# 更新收藏数量
		$result = M('DocumentArticle')->where(array('id'=>$articleId))->setInc('bookmark', 1);
		if(is_numeric($result)){
			$ajaxResult['error'] = 0;
			$ajaxResult['msg'] = "收藏成功!";
			die(json_encode($ajaxResult));
		}
		# END-更新收藏数量
		$ajaxResult['error'] = -4;
		$ajaxResult['msg'] = "更新收藏数量失败!";
		die(json_encode($ajaxResult));
	}

    public function wxShare()
    {
        $arr=array();
        $askUrl=I('askUrl'); 
        $jssdk = new Jssdk('wxb257a3c44afd51f5', '90a94531eb1f83dc6a493b77a67c9485'); 
    	$signPackage = $jssdk->GetSignPackage(urldecode($askUrl));
    	if($signPackage){
    		$this->ajaxReturn($signPackage,'JSON');
    	}else{
    		return false;
    	}
	}

}
